﻿#include<cassert>

//using namespace std;
namespace str_a
{
	class string
	{
		friend ostream& operator<<(ostream& _cout, const string& s);
		friend istream& operator>>(istream& _cin, string& s);
	public:
		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin() const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}

		string(const char* str = "")
			:_size(strlen(str))
			, _capacity(_size)
		{
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		/*string(const string& s1) {
			_str = new char[s1._capacity + 1];
			strcpy(_str, s1._str);
			_size = s1._size;
			_capacity = s1._capacity;
		}*/

		string(const string& s)
		:_str(nullptr)
		,_size(0)
		, _capacity(0)
		{
			string tmp(s._str);
			swap(tmp);
		}

		/*string& operator=(const string& s) {
			if (this != &s) {
				char* tmp = new char[s._capacity + 1];
				strcmp(tmp, s._str);
				delete[] _str;
				_str = tmp;
				_size = s._size;
				_capacity = s._capacity;

			}
			return *this;
		}*/

		string& operator=(const string& s) {
			
			if (this != &s) {
				string tmp(s);
				swap(tmp);
			}
			return *this;
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}


		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size);

			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size);

			return _str[pos];
		}

		size_t capacity() const
		{
			return _capacity;
		}

		size_t size() const
		{
			return _size;
		}

		const char* c_str() const
		{
			return _str;
		}

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;

				_capacity = n;
			}
		}

		void push_back(char ch)
		{
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}

			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}

		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

			strcpy(_str + _size, str);
			_size += len;
		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}

		/*void chack_capacity() {
			int old = _capacity;
			if (_size >= _capacity) {
				while (_capacity < _size) {
					_capacity *= 2;
				}
				char* newstr = new char[_capacity];
				for (size_t i = 0;i < old;i++) {
					newstr[i] = _str[i];
				}
				delete[] _str;
				_str = newstr;
			}
		}*/
		// insert(0, 'x')
		string& insert(size_t pos, char ch)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}
			size_t end = _size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				--end;
			}

			_str[pos] = ch;
			_size++;
			return *this;
		}

		string& insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			size_t len = strlen(str);
			if (_size + len > _capacity) {
				reserve(_size + len);
			}
			/*for (size_t i = size() - 1;i >= pos + len;i--) {
				_str[i] = _str[i - len];
			}*/
			int end = _size;
			while (end >= pos) {
				_str[end + len] = _str[end];
				--end;
			}
			for (size_t i = 0;i < len;i++) {
				_str[i + pos] = str[i];
			}

			_size += len;
			return *this;
		}

		void erase(size_t pos, size_t len = npos)
		{
			assert(pos <= _size);

			if (len == npos || pos + len >= _size)
			{
				_size = pos;

				_str[_size] = '\0';
			}
			else
			{
				size_t end = pos + len;
				while (end <= _size)
				{
					_str[pos++] = _str[end++];
				}
				_size -= len;
			}
		}

		void resize(size_t n, char c = '\0') {
			
			if (n <= _size) {
				_str[n] = '\0';
				_size = n;
			}
			else {
				reserve(n);
				while (_size < n) {
					_str[_size] = c;
					++_size;
				}
				_str[_size] = '\0';
			}
		}

		// 返回c在string中第一次出现的位置

		size_t find(char c, size_t pos = 0) const {
			for (size_t i = pos;i < _size;++i) {
				if (_str[i] == c) {
					return i;
				}
			}
			return npos;
		}

		// 返回子串s在string中第一次出现的位置

		size_t find(const char* s, size_t pos = 0) const {
			const char* p = strstr(_str, s);
			if (p) {
				return p - _str;
			}
			else {
				return npos;
			}
		}

		string substr(size_t pos,size_t len = npos) {
			string s;
			size_t end = pos + len;
			if (len == npos || pos + len >= _size) {
				len = _size - pos;
				end = _size;
			}
			s.reserve(len);
			for (size_t i = pos; i < end;i++) {
				s += _str[i];
			}
			return s;
		}

		bool operator<(const string& s) const
		{
			return strcmp(_str, s._str) < 0;
		}

		bool operator==(const string& s) const
		{
			return strcmp(_str, s._str) == 0;
		}

		bool operator<=(const string& s) const
		{
			return *this < s || *this == s;
		}

		bool operator>(const string& s) const
		{
			return !(*this <= s);
		}

		bool operator>=(const string& s) const
		{
			return !(*this < s);
		}

		bool operator!=(const string& s) const
		{
			return !(*this == s);
		}

		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}
	private:
		char* _str;
		size_t _size;
		size_t _capacity;
		const static size_t npos;
	};

	const size_t string::npos = -1;

	ostream& operator<<(ostream& out, const string& s)
	{
		for (auto ch : s)
			out << ch;
		return out;
	}

	istream& operator>>(istream& in, string& s)
	{
		/*s.clear();
		char ch;
		ch = in.get();
		s.reserve(128);
		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			ch = in.get();
		}
		return in;*/
		s.clear();
		char buff[129];
		size_t i = 0;
		char ch;
		ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == 128) {
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
		}
		if (i != 0) {
			buff[i] = '\0';
			s += buff;
		}
		return in;
	}

	//void test_string1()
	//{
	//	string s1("hello world");
	//	cout << s1.c_str() << endl;

	//	string s2;
	//	cout << s2.c_str() << endl;

	//	for (size_t i = 0; i < s1.size(); i++)
	//	{
	//		cout << s1[i] << " ";
	//	}
	//	cout << endl;

	//	string::iterator it = s1.begin();
	//	while (it != s1.end())
	//	{
	//		(*it)++;
	//		cout << *it << " ";
	//		++it;
	//	}
	//	cout << endl;

	//	for (auto& ch : s1)
	//	{
	//		ch++;
	//		cout << ch << " ";
	//	}
	//	cout << endl;

	//	cout << s1.c_str() << endl;
	//}

	//void test_string2()
	//{
	//	string s1("hello world");
	//	cout << s1.c_str() << endl;
	//	s1.push_back(' ');
	//	s1.append("hello bit hello bit");

	//	cout << s1.c_str() << endl;

	//	s1 += '#';
	//	s1 += "*********************";
	//	cout << s1.c_str() << endl;

	//	string s2;
	//	s2 += '#';
	//	s2 += "*********************";
	//	cout << s2.c_str() << endl;
	//}

	//void test_string3()
	//{
	//	string s1("hello world");
	//	cout << s1.c_str() << endl;

	//	s1.insert(5, '%');
	//	cout << s1.c_str() << endl;

	//	s1.insert(s1.size(), '%');
	//	cout << s1.c_str() << endl;

	//	s1.insert(0, '%');
	//	cout << s1.c_str() << endl;
	//}

	//void test_string4()
	//{
	//	string s1("hello world");
	//	string s2("hello world");

	//	cout << (s1 >= s2) << endl;

	//	s1[0] = 'z';
	//	cout << (s1 >= s2) << endl;

	//	cout << s1 << endl;
	//	cin >> s1;
	//	cout << s1 << endl;

	//	/*char ch1, ch2;
	//	cin >> ch1 >> ch2;*/
	//}

	void test() {
		string s1 = "hello";
		cout << s1 << endl;
		cout << s1.size() << endl;
		cout << s1.capacity() << endl;
		s1.insert(1, " world!");
		cout << s1 << endl;
		cout << s1.size() << endl;
		cout << s1.capacity() << endl;
	}
}